<?php 
	require_once('../params.inc');
	require_once('../data.inc');
	require_once('../utils.inc');
	require_once('sf.inc');
	
	$catname = $par->get('name');
	$thisCat = new Category($catname);
	$tname = $thisCat->tableName();
	$target = urldecode($par->get('target'));
	
	$path = array(
		array('Главная','index.php'),
		array($thisCat->descr(),$target),
		$par->exist('keys') ? 'Редактирование' : 'Добавление'
	);

	require_once('header.inc'); 
?>
	
<script lang=javascript>
	fields = new Array();
	fieldsDescr = new Array();
	
	function deleteFile(owner_name, fname, keys) 
	{
		if (confirm("Вы уверены что хотите удалить файл?")) 
		{
			var opEl = document.getElementById('file_'+fname);
			opEl.parentNode.removeChild( opEl );
			openWindow(
				'delfile.php?name=' + owner_name  + 
				'&fname=' + fname + 
				'&keys=' + keys
			);
		}
	}
</script>


<table width=100%>
<form id=oForm action='op.php' method=post onsubmit="return (onOk(this));" ENCTYPE="multipart/form-data">
	
	<?php
		echo "<input type=hidden name=name value='$catname' />";
		echo "<input type=hidden name=target value=\"$target\" />";
		
		$keyFields = null;
		if ($par->exist('keys')) {
			$keyFields = new DecodedFieldValues($par->get('keys'));
			echo "<input type=hidden name=keys value=\"" . $keyFields->asParamString() . "\" />";
		}
		
		$fields = array();
		
		$q = new Select($db, $tname);
		$flds = new Fields($catname);
		$flds->open();
		while ($flds->fetch()) {
			if ($flds->isEditable()) {
				$posInSrc = -1;
				if ($flds->type() != FIELD_TYPE_FILE) {
					$fname = $flds->name();
					$posInSrc = $q->addFields($fname) - 1;
				}
				array_push(
					$fields,
					new FieldControl(
						$flds->name(),
						$flds->descr(),
						$flds->type(),
						$flds->defaultValue(),
						$flds->length(),
						$flds->fmt(),
						$flds->isNullAllowed(),
						$posInSrc,
						$flds->hasHelp()
					)
				);
			}
		}
		
		if ($keyFields) {
			foreach ($keyFields->asArray() as $fname => $value) {
				$q->where($fname, $value);
			}
			$q->run();
			$q->fetch();
			for ($i=0; $i<count($fields); ++$i) {
				$fields[$i]->setValue($q);
			}
		}
		
		$fieldTxt = <<<EOD
				<tr>
					<td style="width=10px;"></td>
					<td align=right nowrap><b>{NAME}:</b></td>
					<td nowrap width=100%>
						{CONTROL}
					</td>
					<td style="width=10px;">{HELP}</td>
				</tr>
EOD;
				
		foreach ($fields as $field) {
			$field->render($fieldTxt);
		}
	?>
</table>
</form>

<script lang=javascript>
	function submit() {
		var oF = document.getElementById('oForm');
		if (oF.onsubmit()) {
			oF.submit();
		}
	}
	
	function onOk(oForm) 
	{
		for (var i=0; i<fields.length; ++i) 
		{
			if (window[fields[i]].isUserInputValid()) 
			{
				oForm[fields[i]].value = 
					(window[fields[i]].isNull() == false)?
						window[fields[i]].value() :
						'';
			}
			else {
				alert("Пустое или неправильное значение в поле '" + fieldsDescr[i] + "'");
				window[fields[i]].setFocus();
				return false;
			}
		}
		return true;
	}
</script>

<table width=100%>
	<tr>
		<td width=100% align=right>
			<button onclick="top.location.href='<?php echo $target?>'">Отменить</button>
			&nbsp;&nbsp;
			<button onclick="submit()">Сохранить</button>
		</td>
	</tr>
</table>


<?php require_once('footer.inc'); ?>